package com.sdhs.paas.authserver.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdhs.paas.authserver.entity.Client;
import com.sdhs.paas.authserver.entity.ClientInfo;
import com.sdhs.paas.authserver.entity.ClientService;
import com.sdhs.paas.authserver.repository.ClientMapper;
import com.sdhs.paas.authserver.repository.ClientServiceMapper;
import com.sdhs.paas.authserver.service.AuthClientService;
import com.sdhs.paas.authserver.util.client.ClientTokenUtil;
import com.sdhs.paas.authshare.exception.auth.ClientInvalidException;
import com.sdhs.paas.sdk.core.mybatis.EnablePaasMybatis;

@EnablePaasMybatis
@MapperScan("com.sdhs.paas.authserver.repository")
@Service
public class AuthClientServiceImpl implements AuthClientService {

	@Autowired
	private ClientTokenUtil clientTokenUtil;

	@Autowired
	private ClientMapper clientMapper;
	
	@Autowired
	private ClientServiceMapper clientServiceMapper;


	@Override
	public String apply(String clientId, String secret) throws Exception {
		Client client = getClient(clientId, secret);
		return clientTokenUtil
				.generateToken(new ClientInfo(client.getCode(), client.getName(), client.getId().toString()));
	}

	private Client getClient(String clientId, String secret) {
		Client client = new Client();
		client.setCode(clientId);
		client = clientMapper.selectOne(client);
		if (client == null || !client.getSecret().equals(secret)) {
			throw new ClientInvalidException("Client not found or Client secret is error!");
		}
		return client;
	}

	@Override
	public List<String> getAllowedClient(String clientId, String secret) {
		Client info = this.getClient(clientId, secret);
        List<String> clients = clientMapper.selectAllowedClient(info.getId() + "");
        if(clients==null) {
            new ArrayList<String>();
        }
        return clients;
	}

	@Override
	public List<String> getAllowedClient(String serviceId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void registryClient() {
		// TODO Auto-generated method stub

	}

	@Override
	public void validate(String clientId, String secret) throws Exception {
		Client client = new Client();
        client.setCode(clientId);
        client = clientMapper.selectOne(client);
        if(client==null||!client.getSecret().equals(secret)){
            throw new ClientInvalidException("Client not found or Client secret is error!");
        }

	}
	
	@Override
	public List<Client> getClientServices(int id) {
		return clientMapper.selectAuthorityServiceInfo(id);
	}

	@Override
	public int modifyClientServices(int id, String clients) {
		clientServiceMapper.deleteByServiceId(id);
		if (!StringUtils.isEmpty(clients)) {
			String[] mem = clients.split(",");
			for (String m : mem) {
				ClientService clientService = new ClientService();
				clientService.setServiceId(m);
				clientService.setClientId(id + "");
				clientServiceMapper.insertClientService(clientService);
			}
		}
		return 0;
	}
}
